package com.sun.module_home.ui.questions

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
import androidx.compose.material3.adaptive.layout.AnimatedPane
import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffoldRole
import androidx.compose.material3.adaptive.navigation.NavigableListDetailPaneScaffold
import androidx.compose.material3.adaptive.navigation.rememberListDetailPaneScaffoldNavigator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import com.sun.module_base.app.ResultState
import com.sun.module_common.model.Article
import com.sun.module_common.model.ListWrapper
import com.sun.module_common.model.WebModel
import com.sun.module_common.webview.WebScreen
import com.sun.module_common.widget.RefreshScreen
import kotlinx.coroutines.launch

/**
 *@author:sunc
 *@time :2025/8/1 14:51
 *@desc: 问答
 */

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
fun QuestionsScreen(
    modifier: Modifier = Modifier,
    viewModel: QuestionsViewModel = hiltViewModel<QuestionsViewModel>()
) {
    val list = remember { mutableStateListOf<Article>() }

    LaunchedEffect(key1 = Unit) {
        viewModel.articleFlow.collect {
            when (it) {
                is ResultState.Loading -> {}
                is ResultState.Error -> {}
                is ResultState.Success<ListWrapper<Article>?> -> {
                    if (viewModel.isRefresh) {
                        list.clear()
                    }
                    it.data?.datas?.let { item ->
                        list.addAll(item)
                    }
                }
            }
        }
    }


    val navigator = rememberListDetailPaneScaffoldNavigator<Article>()
    val scope = rememberCoroutineScope()

    NavigableListDetailPaneScaffold(
        modifier = modifier then Modifier.fillMaxSize(),
        navigator = navigator,
        listPane = {
            AnimatedPane {
                RefreshScreen(
                    dataList = list,
                    isRefreshing = viewModel.isRefresh,
                    isLoadMore = viewModel.isLoadMore,
                    onRefresh = {
                        viewModel.refresh()
                    },
                    onLoadMore = {
                        viewModel.loadMore()
                    }, itemClick = { item ->
                        scope.launch {
                            navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, item)
                        }
                    })
            }
        },
        detailPane = {
            AnimatedPane {
                navigator.currentDestination?.contentKey?.let {
                    WebScreen(webData = WebModel(it.title, it.link)) {
                        scope.launch {
                            navigator.navigateBack()
                        }
                    }
//                    DetailScreen(data = it) {
//                        scope.launch {
//                            navigator.navigateBack()
//                        }
//                    }
                }

            }
        }
    )
}